<?php

namespace app\admin\controller\userwork;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Festival extends Base
{

    protected $beforeActionList = [
        'first'
    ];

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        return view();
    }

    protected $project_user = 0;

    protected function first()
    {
        $group = DB::name('auth_group_access')->where('uid', session('uid'))->select();
        $group_ids = array_column($group, 'group_id');
        // dump($group_ids);
        //判断当前用户身份
        if (in_array(13, $group_ids) || in_array(1, $group_ids)) {
            //管理员
            $this->project_user = 0;
        } else {
            //开发人员
            $this->project_user = session('uid');
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = new \app\admin\validate\Festival();

            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                $day = explode(' - ', $post['s_time']);

                $s_time = strtotime($day[0]);
                $e_time = strtotime($day[1]);
                $day_num = round($e_time - $s_time) / 86400 + 1;
                $data = [];
                for ($i = 1; $i <= $day_num; $i++) {
                    $item = [];
                    $item['d_time'] = $s_time + 86400 * ($i - 1);
                    $item['info'] = $post['info'];
                    $data[] = $item;
                }
                DB::name('festival')->insertAll($data);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {

            return view();
        }
    }

    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            Db::startTrans();
            try {
                $info = Db::name('festival')->where(['id' => $post['id']])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function list()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if ($this->project_user == 0) {
            if (empty($post['uid']) || $post['uid'] == '') {
            } else {
                $map[] = ['workreg_d.uid', '=', $post['uid']];
            }
        } else {
            $map[] = ['user_work.uid', '=', $this->project_user];
        }
        $data['count'] = Db::name('festival')
            ->where($map)
            ->count();
        $data['data'] = Db::name('festival')
            ->where($map)
            // ->fetchSql(true)
            ->order('d_time asc')->page(input("param.page"), input('param.limit'))->select();
        foreach ($data['data'] as &$info) {
            $info['d_time'] = date('Y-m-d', $info['d_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


}